package com.neuedu.elm.dao.Impl;

import com.neuedu.elm.dao.IBusinessDao;
import com.neuedu.elm.po.Business;
import com.neuedu.elm.util.DBUtil;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BusinessDaoImpl implements IBusinessDao {
    @Override
    public List<Business> selectBusiness(String name, String address) {
        List<Business> businesses = new ArrayList<>();
        Connection connection = DBUtil.getConnection();
        String sql = "select businessId,password,businessName,businessAddress,businessExplain,starPrice,deliveryPrice" +
                " from business where 1=1";

        if (name != null && !name.equals("")) {
            sql = sql + " and businessName like '%" + name + "%'";
        }
        if (address != null && !address.equals("")) {
            sql = sql + " and businessAddress like '%" + address + "%'";
        }
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = connection.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                Business business = new Business(rs.getInt(1), rs.getString(2),
                        rs.getString(3), rs.getString(4),
                        rs.getString(5), rs.getFloat(6), rs.getFloat(7));
                businesses.add(business);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return businesses;
    }

    @Override
    public int insertBusiness(Business business) {
        Connection connection = DBUtil.getConnection();
        int rs = 0;
        String sql = "insert into business (password,businessName,businessAddress,businessExplain,starPrice,deliveryPrice) " +
                "values(?,?,?,?,?,?)";
        PreparedStatement ps = null;
        try {
            ps = connection.prepareStatement(sql);
            ps.setString(1, business.getPassword());
            ps.setString(2, business.getBusinessName());
            ps.setString(3, business.getBusinessAddress());
            ps.setString(4, business.getBusinessExplain());
            ps.setFloat(5, business.getStarPrice());
            ps.setFloat(6, business.getDeliveryPrice());
            rs = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            System.err.println("发生错误，可能商家已存在");
        } finally {
            DBUtil.close(ps, connection);
        }
        return rs;
    }

    @Override
    public int deleteBusiness(int businessId) {
        Connection connection = DBUtil.getConnection();
        int rs = 0;
        String sql1 = "delete from food where businessId=?";
        String sql2 = "delete from business where businessId=?";

        PreparedStatement ps = null;
        try {
            connection.setAutoCommit(false);
            ps = connection.prepareStatement(sql1);
            ps.setInt(1, businessId);
            ps.executeUpdate();
            ps = connection.prepareStatement(sql2);
            ps.setInt(1, businessId);
            rs = ps.executeUpdate();
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            System.err.println("发生错误，删除失败");
            try {
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        } finally {
            DBUtil.close(ps, connection);

        }
        return rs;
    }

    @Override
    public Business login(String name, String pwd) {
        Connection connection = DBUtil.getConnection();
        String sql = "select businessId,password,businessName,businessAddress,businessExplain,starPrice,deliveryPrice from business where password=? and businessName=?";
        ResultSet rs = null;
        Business business = new Business();
        PreparedStatement ps = null;
        try {
            ps = connection.prepareStatement(sql);
            ps.setString(1, pwd);
            ps.setString(2, name);
            rs = ps.executeQuery();
            while (rs.next()) {
                Business business1 = new Business(rs.getInt(1), rs.getString(2),
                        rs.getString(3), rs.getString(4),
                        rs.getString(5), rs.getFloat(6), rs.getFloat(7));
                business = business1;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            System.err.println("发生错误，修改失败");

        } finally {
            DBUtil.close(ps, connection);

        }
        return business;
    }

    @Override
    public Business queryBusiness(int businessId) {
        Connection connection = DBUtil.getConnection();
        String sql = "select businessId,password,businessName,businessAddress,businessExplain,starPrice,deliveryPrice from business where businessId=?";
        ResultSet rs = null;
        Business business = new Business();
        PreparedStatement ps = null;
        try {
            ps = connection.prepareStatement(sql);
            ps.setInt(1, businessId);
            rs = ps.executeQuery();
            while (rs.next()) {
                Business business1 = new Business(rs.getInt(1), rs.getString(2),
                        rs.getString(3), rs.getString(4),
                        rs.getString(5), rs.getFloat(6), rs.getFloat(7));
                business = business1;
            }


        } catch (SQLException e) {
            e.printStackTrace();
            System.err.println("发生错误，修改失败");

        } finally {
            DBUtil.close(ps, connection);

        }

        return business;
    }

    @Override
    public int updateBusiness(Business business) {
        System.out.println(business);
        Connection connection = DBUtil.getConnection();
        StringBuilder sql = new StringBuilder("UPDATE business SET ");
        List<Object> params = new ArrayList<>();

        if (business.getBusinessName() != null && !business.getBusinessName().isEmpty()) {
            sql.append("businessName = ?, ");
            params.add(business.getBusinessName());
        }
        if (business.getBusinessAddress() != null && !business.getBusinessAddress().isEmpty()) {
            sql.append("businessAddress = ?, ");  // 添加这个字段
            params.add(business.getBusinessAddress());
        }
        if (business.getBusinessExplain() != null && !business.getBusinessExplain().isEmpty()) {
            sql.append("businessExplain = ?, ");
            params.add(business.getBusinessExplain());
        }
        if (business.getStarPrice() != null) {
            sql.append("starPrice = ?, ");
            params.add(business.getStarPrice());
        }
        if (business.getDeliveryPrice() != null) {
            sql.append("deliveryPrice = ?, ");
            params.add(business.getDeliveryPrice());
        }

        sql.delete(sql.length() - 2, sql.length());
        sql.append(" WHERE businessId = ?");
        params.add(business.getBusinessId());

        System.out.println("Generated SQL: " + sql.toString());
        System.out.println("Parameters: " + params);

        PreparedStatement ps = null;
        int result = 0;
        try {
            ps = connection.prepareStatement(sql.toString());
            for (int i = 0; i < params.size(); i++) {
                ps.setObject(i + 1, params.get(i));
            }
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            System.err.println("发生错误，修改失败");
        } finally {
            DBUtil.close(ps, connection);
        }
        return result;
    }


    @Override
    public int updateBusinessPwd(int businessId, String newpwd) {
        Connection connection = DBUtil.getConnection();
        String sql = "update business set password=? where BusinessId=?";
        PreparedStatement ps = null;
        int rs = 0;
        try {
            ps = connection.prepareStatement(sql);
            ps.setString(1,newpwd);
            ps.setInt(2, businessId);
            rs = ps.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
            System.err.println("发生错误，修改失败");

        } finally {
            DBUtil.close(ps, connection);

        }
        return rs;
    }
}
